"""
知识库文件分享相关数据模型
"""
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field  # pyright: ignore[reportMissingImports]
from bson import ObjectId  # pyright: ignore[reportMissingImports]
from app.models.base import PyObjectId


class KnowledgeShare(BaseModel):
    """知识库文件分享模型"""
    id: Optional[PyObjectId] = Field(default=None, alias="_id")
    file_id: PyObjectId = Field(..., description="文件ID")
    token: str = Field(..., description="分享令牌")
    share_url: str = Field(..., description="分享URL")
    allow_download: bool = Field(default=True, description="是否允许下载")
    expires_at: Optional[datetime] = Field(default=None, description="过期时间")
    access_count: int = Field(default=0, description="访问次数")
    max_access_count: Optional[int] = Field(default=None, description="最大访问次数")
    password: Optional[str] = Field(default=None, description="访问密码（可选）")
    created_by: PyObjectId = Field(..., description="创建者ID")
    created_at: datetime = Field(default_factory=datetime.utcnow, description="创建时间")
    revoked_at: Optional[datetime] = Field(default=None, description="撤销时间")

    class Config:
        populate_by_name = True
        arbitrary_types_allowed = True
        json_encoders = {
            ObjectId: str,
            datetime: lambda v: v.isoformat()
        }

